/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

/**
 *
 * @author TienNT
 */
public class Date {

    private int mYear;
    private int mMonth;
    private int mDay;
    // Date format: dd/mm/yyyy

    public Date(int day, int month, int year) {
        mDay = day;
        mMonth = month;
        mYear = year;
    }

    public int getYear() {
        return mYear;
    }

    public void setYear(int mYear) {
        this.mYear = mYear;
    }

    public int getMonth() {
        return mMonth;
    }

    public void setMonth(int mMonth) {
        this.mMonth = mMonth;
    }

    public int getDay() {
        return mDay;
    }

    public void setDay(int mDay) {
        this.mDay = mDay;
    }

    public static Date parseDate(String date) {
        String[] data = date.split("/");
        if (data.length == 3) {
            try {
                int year = Integer.parseInt(data[2]);
                if (year < 0) {
                    return null;
                }
                int month = Integer.parseInt(data[1]);
                if (month > 12 || month < 0) {
                    return null;
                }
                int day = Integer.parseInt(data[0]);
                if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
                    if (day > 31) {
                        return null;
                    }
                } else if (month == 2) {
                    if (year % 4 == 0) {
                        if (day > 29) {
                            return null;
                        }
                    } else {
                        if (day > 28) {
                            return null;
                        }
                    }
                } else {
                    if (day > 30) {
                        return null;
                    }
                }
                return new Date(day, month, year);
            } catch (NumberFormatException ex) {
                return null;
            }
        }
        return null;
    }

    public boolean equals(Date date) {
        if (mDay == date.getDay() && mMonth == date.getMonth() && mYear == date.getYear()) {
            return true;
        }
        return false;
    }

    public int compare(Date date) {
        if (mYear < date.getYear()) {
            return -1;
        } else if (mYear > date.getYear()) {
            return 1;
        } else {
            if (mMonth < date.getMonth()) {
                return -1;
            } else if (mMonth > date.getMonth()) {
                return 1;
            } else {
                if (mDay < date.getDay()) {
                    return -1;
                } else if (mDay > date.getDay()) {
                    return 1;
                }
            }
        }
        return 0;
    }

    @Override
    public String toString() {
        String result = "";
        result += mDay < 10 ? "0" + mDay : mDay;
        result += "/";
        result += mMonth < 10 ? "0" + mMonth : mMonth;
        result += "/";
        result += mYear < 10 ? "0" + mYear : mYear;

        return result;
    }
}
